package com.smartleanx.module.hrm.intergration;

import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.system.api.user.AdminUserApi;
import com.smartleanx.module.system.api.user.dto.AdminUserPageReqDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserSaveReqDTO;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户服务
 *
 * @author tengcj
 * @since 2024/5/11
 */
@Component
public class AdminUserServiceProvider {

    @Resource
    private AdminUserApi adminUserApi;

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    public AdminUserRespDTO getAdminUser(Long id) {
        return adminUserApi.getUser(id).getCheckedData();
    }

    public <T> List<T> creatorAndUpdaterMapToNickname(List<T> list) {
        return adminUserApi.creatorAndUpdaterMapToNickname(list);
    }

    /**
     * 根据部门id获取所有用户
     *
     * @param deptIds
     * @return
     */
    public List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds) {
        return adminUserApi.getUserListByDeptIds(deptIds).getCheckedData();
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    public AdminUserRespDTO getLoginUser() {
        return adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId()).getCheckedData();
    }

    /**
     * 通过用户 ID 查询用户
     * @param ids 用户编号数组
     * @return 用户列表
     */
    public List<AdminUserRespDTO> getUserList(@RequestParam("ids") Collection<Long> ids){
        return adminUserApi.getUserList(ids).getCheckedData();
    }

    /**
     * 批量获取用户信息(包含逻辑删除的)
     *
     * @param contactIds
     * @return Map
     */
    public Map<Long, AdminUserRespDTO> getAllUserMap(Set<Long> contactIds) {
        return adminUserApi.getAllUserMap(contactIds);
    }

    /**
     * 员工调动时，同步用户信息
     *
     * @param saveReqDTO
     * @return
     */
    public Boolean updateAdminUser(AdminUserSaveReqDTO saveReqDTO) {
        return adminUserApi.updateUser(saveReqDTO).getCheckedData();
    }

    /**
     * 员工调动时，同步用户信息
     *
     * @param saveReqDTO
     * @return
     */
    public Long createAdminUser(AdminUserSaveReqDTO saveReqDTO) {
        return adminUserApi.createUser(saveReqDTO).getCheckedData();
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    public Boolean deleteAdminUser(Long id) {
        return adminUserApi.deleteUser(id).getCheckedData();
    }

    /**
     * 获取所有用户
     * @return
     */
    public List<AdminUserRespDTO> getAllUserList(String name,Boolean ignoreDataPermission){
        return adminUserApi.getUserList(name,ignoreDataPermission).getCheckedData();
    }


    /**
     * 通过岗位id拿到所有用户id
     * @return
     */
    public List<Integer> getUserListByPostId(List<Long> postIds){
        List<AdminUserRespDTO> checkedData = adminUserApi.getUserListByPostIds(postIds).getCheckedData();
        if(checkedData.isEmpty()){
            return null;
        }else {
            List<Integer> collect = checkedData.stream().map(adminUserRespDTO -> adminUserRespDTO.getId().intValue()).collect(Collectors.toList());
        return collect;
        }
    }
    public PageResult<AdminUserRespDTO> getUserPage(AdminUserPageReqDTO pageReqDTO) {
        return adminUserApi.getUserPage(pageReqDTO).getCheckedData();
    }

}
